#include "Window.hpp"
#include "Engine.hpp"
#include "GUI.hpp"
const Engine* ENGINE;


int main(const int argc, const char* argv[])
{
	//Engine engine(initWindow(), argc >= 2 ? argv[1] : "");
	Engine engine(initWindow(), "../../../");
	ENGINE = &engine;
	//GUI gui(&engine);


	auto cb = engine.addObject3D("cb", "models/cube.obj");
	auto floor = engine.addObject3D("cb", "models/cube.obj");
	auto bp = engine.addObject3D("bp", "models/backpack/backpack.obj");

	auto ol  = engine.addOmniLight3D("ol",  2.f, vec3(1.f), "models/cube.obj");
	auto ol2 = engine.addOmniLight3D("ol2", 5.f, vec3(1.f), "models/cube.obj");

	auto sl  = engine.addSpotlight3D("sl",  glm::vec2(cosf(glm::radians(6.f)), 4.f),  vec3(1.f));
	auto sl2 = engine.addSpotlight3D("sl2", glm::vec2(cosf(glm::radians(6.f)), 40.f), vec3(1.f), "models/cube.obj");


	ol->scale(glm::vec3(.4f));
	sl2->scale(glm::vec3(0.2f));
	sl2->teleport(glm::vec3(0.f, 2.f, 0.f));
	cb->teleport(glm::vec3(2.f, 2.f, -4.f));
	floor->scale(glm::vec3(100.f, .005f, 100.f));
	

	unsigned c[] = { 0, 1 };
	const float& time = engine.time;

	while (!glfwWindowShouldClose(engine.camera->window))
	{
		ol->teleport(glm::vec3(cos(time), 1.f, sin(time)) * 3.f + bp->getPosition());
		ol->lookAt(bp->getPosition());

		ol2->teleport(glm::vec3(-cos(time), sin(time), cos(time)) * 6.f + glm::vec3(0.f, 2.f, 0.f));
		ol2->lookAt(cb->getPosition());

		bp->lookAt(engine.camera->getPosition());
		bp->teleport(glm::vec3(sin(time), 0.f, 0.f) * 8.f + glm::vec3(0.f, 2.f, 0.f));

		sl->teleport(engine.camera->getPosition());
		sl->setDirection(engine.camera->getDirection());

		sl2->teleport(glm::vec3(cos(time), 1.f, sin(time)) * 3.f + cb->getPosition());
		sl2->lookAt(cb->getPosition() + glm::vec3(0.f, 1.f, 0.f));

		engine.draw();

		glfwSwapBuffers(engine.camera->window);
		engine.activateLight();
		glfwPollEvents();
		c[0] += 1.f / engine.period;
		c[1] += 1;
	}

	printf("Average FPS: %d\n", c[0] / c[1]);
	
	std::cout << glGetError() << std::endl;
	glfwDestroyWindow(engine.camera->window);
	glfwTerminate();
	exit(EXIT_SUCCESS);
}
void mouse_callback(GLFWwindow *window, double x, double y)
{
	ENGINE->camera->rotate_callback((float)x, (float)y);
}
